import GameUtil from '../../Fantasy/Util/GameUtil';
import DataBase from '../../Server/Data/DataBase';
import ServerCenter from '../../Server/ServerCenter';
import QuestSpawn from '../QuestSpawn';
import SoundUtil from '../../Fantasy/Util/SoundUtil';
import PromptManage from '../PromptManage';
import JsbUtil from '../../Fantasy/Util/JsbUtil';
import StorageCenter from '../../Server/StorageCenter';


const { ccclass, property } = cc._decorator;

@ccclass
export default class PKScene extends cc.Component {

    @property(cc.Node)
    RootNode: cc.Node = null;
    @property(cc.Node)
    Success: cc.Node = null;
    @property(cc.Node)
    GoodGame: cc.Node = null;
    @property(cc.Node)
    PKShow: cc.Node = null;
    @property(cc.Node)
    MatchShow: cc.Node = null;
    @property(cc.Prefab)
    wrongPre:cc.Prefab = null
    @property([cc.Label])
    robotName:cc.Label[] = [];
    @property([cc.Sprite])
    robotHead:cc.Sprite[] = [];
    @property(cc.Node)
    successCthg:cc.Node = null;
    @property(cc.Node)
    failCthg:cc.Node = null;


    start() {
        SoundUtil.stopMusic();
        let array = DataBase.RankInfo;
        let i = Math.floor(Math.random()*array.length);
        let robot = array[i];
        for(let cop of  this.robotName){
            cop.string = robot.name;
        }
        for(let cop of this.robotHead){
            LoadResImg(cop,'Texture/Head/'+robot.head)
        }
        this.showStep(1);
        JsbUtil.listenAndroidBack();
    }

    // update (dt) {}
    showStep(index) {
        switch (index) {
            case 1:
                this.MatchShow.active = true;
                this.PKShow.active = false;
                this.Success.active = false;
                this.GoodGame.active = false;
                this.RootNode.active = false;
                break;
            case 2:
                this.MatchShow.active = false;
                this.PKShow.active = true;
                this.Success.active = false;
                this.GoodGame.active = false;
                this.RootNode.active = false;
                break;
            case 3:
                this.MatchShow.active = false;
                this.PKShow.active = false;
                this.Success.active = false;
                this.GoodGame.active = false;
                this.RootNode.active = true;
                this.RootNode.getComponent('PKInfoJs').createRobot();
                break;
        }
    }
    showGameOver(myExp,robotExp){
        ServerCenter.addUserExp(myExp);
        if (myExp >= robotExp){
            this.Success.active = true;
            SoundUtil.playEffect("music/success")
            cc.find('myExp',this.Success).getComponent(cc.Label).string = myExp+'';
            cc.find('robotExp',this.Success).getComponent(cc.Label).string = robotExp+'';
        }
        else{
            this.GoodGame.active = true;
            cc.find('myExp',this.GoodGame).getComponent(cc.Label).string = myExp+'';
            cc.find('robotExp',this.GoodGame).getComponent(cc.Label).string = robotExp+'';
        }
        this.RootNode.active = false;
    }
    btnCallback(target, data) {
        SoundUtil.playEffect("music/click");
        if (data == 'close') {
            PromptManage.addExitDialog(false);
        }
    }
    SuccessBack(target, data) {
        SoundUtil.playEffect("music/click");
        StorageCenter.addUserVictory(1);
        if (data == 'close') {
            this.backMainScene();
        } else if (data == 'change') {
            GameUtil.gotoScene('PKScene');
        } else if (data == 'cthg') {
            this.successCthg.active = false;
            let wrongNode = cc.instantiate(this.wrongPre)
            wrongNode.parent = this.node;
        }
    }
    FailBack(target, data) {
        SoundUtil.playEffect("music/click");
        if (data == 'close') {
            this.backMainScene();
        } else if (data == 'change') {
            GameUtil.gotoScene('PKScene');
        } else if (data == 'cthg') {
            this.failCthg.active = false;
            let wrongNode = cc.instantiate(this.wrongPre)
            wrongNode.parent = this.node;
        } else if (data == 'again') {
            this.showStep(2);
        }
    }
    backMainScene() {
        SoundUtil.playEffect("music/click");
        GameUtil.gotoScene('MainScene');
    }
}
